/*
 *
 * Copyright 2020 Asylo authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#ifndef ASYLO_IDENTITY_ATTESTATION_SGX_SGX_AGE_REMOTE_ASSERTION_VERIFIER_H_
#define ASYLO_IDENTITY_ATTESTATION_SGX_SGX_AGE_REMOTE_ASSERTION_VERIFIER_H_

#include <memory>
#include <string>
#include <vector>

#include "asylo/crypto/certificate.pb.h"
#include "asylo/crypto/certificate_interface.h"
#include "asylo/crypto/certificate_util.h"
#include "asylo/identity/attestation/enclave_assertion_verifier.h"
#include "asylo/identity/identity.pb.h"
#include "asylo/identity/identity_acl.pb.h"
#include "asylo/util/mutex_guarded.h"
#include "asylo/util/status.h"
#include "asylo/util/statusor.h"

namespace asylo {

/// An implemention of the EnclaveAssertionVerifier interface for SGX remote
/// assertions generated by the Assertion Generator Enclave.
///
/// An SgxAgeRemoteAssertionVerifier is capable of verifying assertions of SGX
/// code identity that originate from an Assertion Generator Enclave that has a
/// compatible set of root certificates.
class SgxAgeRemoteAssertionVerifier final : public EnclaveAssertionVerifier {
 public:
  /// Constructs an uninitialized SgxAgeRemoteAssertionVerifier.
  ///
  /// The verifier must be initialized via a call to Initialize().
  SgxAgeRemoteAssertionVerifier() = default;

  ///////////////////////////////////////////
  //   From AssertionAuthority interface.  //
  ///////////////////////////////////////////

  Status Initialize(const std::string &config) override;

  bool IsInitialized() const override;

  EnclaveIdentityType IdentityType() const override;

  std::string AuthorityType() const override;

  ///////////////////////////////////////////
  //    From AssertionVerifier interface.  //
  ///////////////////////////////////////////

  Status CreateAssertionRequest(AssertionRequest *request) const override;

  StatusOr<bool> CanVerify(const AssertionOffer &offer) const override;

  Status Verify(const std::string &user_data, const Assertion &assertion,
                EnclaveIdentity *peer_identity) const override;

 private:
  // Struct that holds class members to be guarded by the initialization mutex.
  struct Members {
    // The Intel root cert.
    std::unique_ptr<CertificateInterface> intel_root_certificate;

    // Required root certificates outside of the Intel root cert.
    CertificateInterfaceVector additional_root_certificates;

    // A predicate for accepted AGE identities.
    IdentityAclPredicate age_identity_expectation;

    // The assertion request that is sent to peers.
    AssertionRequest assertion_request;

    // Indicates whether this generator has been initialized.
    bool initialized;

    Members() : initialized(false) {}
  };

  MutexGuarded<Members> members_;
};

}  // namespace asylo

#endif  // ASYLO_IDENTITY_ATTESTATION_SGX_SGX_AGE_REMOTE_ASSERTION_VERIFIER_H_
